package com.example.javadesign.prototype.deep;

import lombok.Data;

import java.io.*;
import java.util.List;

/**
 * @author 陳樂
 * @version 1.0.0
 * @ClassName ConcretePrototype.java
 * @Description TODO
 * @createTime 2022年08月21日 20:30:00
 */
@Data
public class ConcretePrototype  implements Cloneable,Serializable {

    private int age;

    private String name;

    private List<String> hobbies;

    @Override
    public ConcretePrototype clone() {
        try {
            return (ConcretePrototype) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

    public ConcretePrototype deepClone() {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);

            return (ConcretePrototype) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
